package com.chookss.video

import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RoundRectShape
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chookss.MyApp
import com.chookss.R
import com.chookss.tools.RecyclerClickAdapter
import com.chookss.tools.Utils
import com.chookss.video.entity.VideoEntity
import com.chookss.video.model.VideoModel
import com.chookss.view.RecycleViewDivider
import com.johnrambo.ktea.ktExtends.observe
import errorToast
import kotlinx.android.synthetic.main.fragment_video_recommends.*
import kotlinx.android.synthetic.main.item_video_recommends_2.view.*
import kotlinx.android.synthetic.main.layout_common_empty.*

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/26 18:48
 * Note:
 */
class VideoRecommendsFragment2 : Fragment() {
    fun VideoRecommendsFragment2() {}
    private var emptyParams: LinearLayout.LayoutParams? = null
    lateinit var adapter:MyAdapter
    private val videoModel by lazy {
        ViewModelProviders.of(this).get(VideoModel::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_video_recommends, null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter = MyAdapter()
        recyclerView.adapter = adapter
        tvEmpty.text="暂无热点数据"
        ivEmpty.setBackgroundResource(R.drawable.icon_default_no_search)
        emptyParams = ivEmpty?.getLayoutParams() as LinearLayout.LayoutParams?
        emptyParams?.setMargins(0, Utils.dip2px(context, 40f), 0, 0)
        ivEmpty?.setLayoutParams(emptyParams)
        recyclerView.addItemDecoration(
            RecycleViewDivider(
                context, LinearLayoutManager.HORIZONTAL, Utils.dip2px(context, 0.5f), getResources().getColor(R.color.line_gray)
            )
        )
        recyclerView.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL, false)
        videoModel.selectRecommend("2").observe(viewLifecycleOwner, {
            if (it.error != null) {
                activity?.errorToast(it.error.message?:"")
            } else {
                adapter.datas = it.data
                adapter.notifyDataSetChanged()
                if(it.data.size==0){
                    recyclerView.visibility=View.GONE
                    llEmpty.visibility=View.VISIBLE
                }
            }
        })
        adapter.setOnItemClickListener { adapter, data, holder ->
            startActivity(Intent(context, VideoSetsPlayActivity::class.java).apply {
                putExtra("video", data)
                putExtra("type", "2")
                putExtra("watermark", "4")
            })
        }
    }

    //#ED5D40 #ED9E40 #EDD740
    class MyAdapter : RecyclerClickAdapter<VideoEntity, RecyclerView.ViewHolder>() {
        companion object {
            fun createDrawBg(color:Int):Drawable {
                var p = Utils.dip2px(MyApp.appCtx, 2f).toFloat()
                var outer = floatArrayOf(p,p,p,p,p,p,p,p)
                var shape = ShapeDrawable(RoundRectShape(outer, null, null))
                shape.paint.setColor(color)
                shape.paint.style = Paint.Style.FILL
                return shape
            }
        }

        var datas: List<VideoEntity>? = null
        val drawables by lazy {
            arrayOf(
                    createDrawBg(0xffED5D40.toInt()),
                    createDrawBg(0xffED9E40.toInt()),
                    createDrawBg(0xffEDD740.toInt())
            )
        }

        override fun getItemCount() = datas?.size ?: 0

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return object:RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_video_recommends_2, parent, false)) {}
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            var view = holder.itemView
            var item = datas!![position]
            view.tvIndex.text = "${position+1}"
            view.tvCount.text = item.videoPlayCounts.toString()+"次"
            view.tvName.text = item.videoCollectionsName
            if (position < drawables.size) {
                view.tvIndex.background = drawables[position]
                view.tvIndex.setTextColor(Color.WHITE)
            } else {
                view.tvIndex.background = null
                view.tvIndex.setTextColor(Color.parseColor("#ff333333"))
            }
        }
        override fun getItem(position: Int) = datas!![position]
    }

}